<template>
  <div>
    <el-dialog title="重置密码" :visible.sync="dialogVisible" width="600px">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="150px"
      >
        <el-form-item label="分站超管密码" prop="pwd">
          <el-input
            v-model="ruleForm.pwd"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item
          v-if="ruleForm.pwd"
          label="分站超管确认密码"
          prop="checkPwd"
        >
          <el-input
            v-model="ruleForm.checkPwd"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Debounce } from "@/utils/validate";
import { putRestPassword } from "@/api/city";
export default {
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        pwd: "",
        checkPwd: "",
      },
      rules: {
        pwd: [
          {
            required: true,
            message: "请输入分站密码",
            trigger: ["blur", "change"],
          },
        ],
        checkPwd: [
          {
            required: true,
            message: "请输入分站确认密码",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    edit(item) {
      this.currentId = item.id;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
        setTimeout(() => {
            this.$refs.ruleForm.clearValidate();
        },30)
      });
    },
    submitForm: Debounce(function () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let { pwd, checkPwd } = this.ruleForm;
          if (pwd !== checkPwd) {
            this.$message.error("两次输入的密码不一致！");
            return;
          }
          putRestPassword({
            id: this.currentId,
            password: pwd,
            checkPassword: checkPwd,
          })
            .then((res) => {
              this.$message.success("重置成功");
              this.dialogVisible = false;
            })
            .catch((err) => {
              this.$message.error("重置失败");
            });
        } else {
          return false;
        }
      });
    }),
  },
};
</script>

<style lang="scss" scoped>
</style>